﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wangxiaoyu_B.Models;

namespace wangxiaoyu_B.Controllers
{
    public class HomeController : Controller
    {
        AirEtermEntities db = new AirEtermEntities();
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 编辑框更新数据
        /// </summary>
        /// <returns></returns>
        public ActionResult update(flights flights)
        {
            var list = db.flights.FirstOrDefault(x => x.fId == flights.fId);
            list.flightNo = flights.flightNo;
            list.departureAirport = flights.departureAirport;
            list.departureTime = flights.departureTime;
            list.price = flights.price;

            int n = db.SaveChanges();
            if (n>0)
            {
                return Json(new { code = 0, msg = "成功" });
            }
            else
            {
                return Json(new { code = -1, msg = "失败" });
            }
        }
        /// <summary>
        /// 编辑显示
        /// </summary>
        /// <returns></returns>
        public ActionResult GetEditData(int fId)
        {
            var list = db.flights.Select(p => new
            {
                fId = p.fId,
                flightNo = p.flightNo,
                departureAirport = p.departureAirport,
                departureTime = p.departureTime.ToString(),
                price = p.price
            }).FirstOrDefault(x => x.fId == fId);
            return Json(new { data = list }, JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 下拉显示
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            var list = db.airports.Select(p => new { aId = p.aId, airportName = p.airportName }).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取表格数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPageList(int page,int limit,string flightNo,string departureAirport)
        {
            var dataList = db.flights.Include("airports").AsEnumerable();
            var count = dataList.Count();

            if (!string.IsNullOrEmpty(flightNo))
            {
                dataList = dataList.Where(p => p.flightNo.Contains(flightNo));
            }
            if (!string.IsNullOrEmpty(departureAirport))
            {
                dataList = dataList.Where(p => p.departureAirport==Convert.ToInt32(departureAirport));
            }

            var list = dataList.Select(p => new
            {
                fId = p.fId,
                flightNo = p.flightNo,
                departureAirport = p.departureAirport,
                airportName = p.airports.airportName,
                arriveAirport = p.arriveAirport,
                arriveAirportname = p.airports.airportName,
                departureTime = p.departureTime.ToString(),
                arriveTime = p.arriveTime.ToString(),
                price = p.price,
                meals = p.meals,
            });

            return Json(new { count = count, data = list.OrderBy(p => p.fId).Skip((page - 1) * limit).Take(limit).ToList() });
        }
    }
}